package com.estate.struts.actions;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.estate.business.ContractFileService;
import com.estate.utils.FileUtil;
import com.estate.vo.Vo;
import com.estate.vo.impl.ContractFileVo;
import com.estate.vo.impl.UserInfoVo;

public class ContractFileAction extends EstateSupport implements
ServletRequestAware, ServletResponseAware {

	private int id;
	private String contractId;
	private String fileSaveName;
	private Date uploadDate;
	private UserInfoVo uploadUser;
	private String filePath;
	private String fileRealName;
	private String saveFile;
	private String saveName;
	private String Filedata;
	private String FiledataFileName;
	private Date receiveDate;
	private String remark;
	private int deleteId;
	
	private String method;
	private ContractFileService contractFileService;
	
	private HttpServletRequest request;
	private HttpServletResponse response;
	
	public String execute() throws Exception {
		
		String page="";
		
		if("upload".equals(method)){
			File directory=new File(FileUtil.COMPANYSTUFF_FILE);
			saveName=FileUtil.getReplaceFileName(this.getFiledataFileName());
			saveFile=directory+File.separator+saveName;
			this.getSession().setAttribute("saveName", saveName);
			this.getSession().setAttribute("saveFile", saveFile);
			this.getSession().setAttribute("realName", this.getFiledataFileName());
			this.getSession().setAttribute("contractId", this.getContractId());
			FileInputStream fis=new FileInputStream(this.getFiledata());
			
			if(!directory.exists()){
				directory.mkdir();
			}
			
			FileOutputStream fos=new FileOutputStream(saveFile);
			byte[] buffer=new byte[1024];
			int length;
			while((length=fis.read(buffer))>0){
				fos.write(buffer,0,length);
			}
			fis.close();
			fos.close();
			System.out.println("out load");
			page="upload";
		} else if("write".equals(method)){
			ContractFileVo vo=new ContractFileVo();
			vo.setContractId(Integer.parseInt(this.getSession().getAttribute("contractId").toString()));
			vo.setFilePath(this.getSession().getAttribute("saveFile").toString());
			vo.setFileRealName(this.getSession().getAttribute("realName").toString());
			vo.setFileSaveName(this.getSession().getAttribute("saveName").toString());
			vo.setUploadUser(this.getSessionUser());
			vo.setUploadDate(uploadDate);
			vo.setRemark(remark);
			vo.setReceiveDate(receiveDate);
			Date date=new Date(System.currentTimeMillis());
			vo.setUploadDate(date);
			contractFileService.addVo(vo);
			this.getRequest().setAttribute("isNameVisible", new Boolean(true));
			this.getRequest().setAttribute("fileName", this.getFiledataFileName());
			page="upload";
		} else if("delete".equals(method)){
			int contract=contractFileService.findProId(this.getDeleteId());
			System.out.println(contract+"~contractId");
			this.setContractId(((Integer)contract).toString());
			contractFileService.deleteById(this.getDeleteId());
			
			page="delete";
		} else if("list".equals(method)){
			List<Vo> voList=contractFileService.getFilesByContractId(Integer.parseInt(this.getContractId()));
			this.getRequest().setAttribute("fileList", voList);
			page="list";
		} else if ("download".equals(method)) {
			response.setContentType("application/x-download");
			String fileDownload = this.getFilePath();
			String filedisplay = this.getFileRealName();
			response.addHeader("Content-Disposition", "attachment;filename="
					+ filedisplay);

			OutputStream outp = null;
			FileInputStream in = null;

			try {
				outp = response.getOutputStream();
				in = new FileInputStream(fileDownload);

				byte[] b = new byte[1024];
				int i = 0;

				while ((i = in.read(b)) > 0) {
					outp.write(b, 0, i);
				}
				outp.flush();
			} catch (Exception e) {
				System.out.println("Error!");
				e.printStackTrace();
			} finally {
				if (in != null) {
					in.close();
					in = null;
				}
				if (outp != null) {
					outp.close();
					outp = null;
				}
			}
			page = null;
		}
		
		return page;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getId() {
		return id;
	}
	public void setContractId(String contractId) {
		this.contractId = contractId;
	}
	public String getContractId() {
		return contractId;
	}
	public void setFileSaveName(String fileSaveName) {
		this.fileSaveName = fileSaveName;
	}
	public String getFileSaveName() {
		return fileSaveName;
	}
	public void setUploadDate(Date uploadDate) {
		this.uploadDate = uploadDate;
	}
	public Date getUploadDate() {
		return uploadDate;
	}
	public void setUploadUser(UserInfoVo uploadUser) {
		this.uploadUser = uploadUser;
	}
	public UserInfoVo getUploadUser() {
		return uploadUser;
	}
	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}
	public String getFilePath() {
		return filePath;
	}
	public void setFileRealName(String fileRealName) {
		this.fileRealName = fileRealName;
	}
	public String getFileRealName() {
		return fileRealName;
	}
	public void setSaveFile(String saveFile) {
		this.saveFile = saveFile;
	}
	public String getSaveFile() {
		return saveFile;
	}
	public void setSaveName(String saveName) {
		this.saveName = saveName;
	}
	public String getSaveName() {
		return saveName;
	}
	public void setFiledata(String filedata) {
		Filedata = filedata;
	}
	public String getFiledata() {
		return Filedata;
	}
	public void setFiledataFileName(String filedataFileName) {
		FiledataFileName = filedataFileName;
	}
	public String getFiledataFileName() {
		return FiledataFileName;
	}
	public void setMethod(String method) {
		this.method = method;
	}
	public String getMethod() {
		return method;
	}
	public void setContractFileService(ContractFileService contractFileService) {
		this.contractFileService = contractFileService;
	}
	public ContractFileService getContractFileService() {
		return contractFileService;
	}
	public void setReceiveDate(Date receiveDate) {
		this.receiveDate = receiveDate;
	}
	public Date getReceiveDate() {
		return receiveDate;
	}
	public void setRemark(String remark) {
		this.remark = remark;
	}
	public String getRemark() {
		return remark;
	}
	public void setDeleteId(int deleteId) {
		this.deleteId = deleteId;
	}
	public int getDeleteId() {
		return deleteId;
	}
	public void setServletRequest(HttpServletRequest request) {
		// TODO Auto-generated method stub
		this.request = request;
	}

	public void setServletResponse(HttpServletResponse response) {
		// TODO Auto-generated method stub
		this.response = response;
	}
}
